package com.cao.project.quartz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cao.project.domain.Task;
import com.cao.project.mapper.TaskMapper;
import com.cao.project.service.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Component
public class TaskChangeStatus {

    //每天凌晨4点执行
    @Autowired
    ITaskService taskService;

    @Scheduled(cron ="0 0 4 ? * *")
    public void chang() {
        LocalDateTime now = LocalDateTime.now();
        LambdaQueryWrapper<Task> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ne(Task::getStatus, "over")  // 状态不等于“结束”
                .lt(Task::getDueDate, now);    // 超时（dueTime小于当前时间）

        List<Task> tasks = taskService.list(queryWrapper);
        // 处理查询到的任务
        tasks.forEach(task -> {
            // 在这里添加处理任务的逻辑
            task.setStatus("overdue");
            System.out.println("处理任务: " + task);
        });
        taskService.updateBatchById(tasks);

    }
}
